package com.buaa.aimall.goodsModule.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.buaa.aimall.Utils.BaseResponse;
import com.buaa.aimall.Utils.Enum.StatusCode;
import com.buaa.aimall.Utils.Exception.BusinessException;
import com.buaa.aimall.Utils.PageReturnResult;
import com.buaa.aimall.goodsModule.Converter.GoodsOrderAppealWorkOrderConvert;
import com.buaa.aimall.goodsModule.DTO.*;
import com.buaa.aimall.goodsModule.Entity.GoodsOrderAppealWorkOrderEntity;
import com.buaa.aimall.goodsModule.Service.GoodsOrderAppealWorkOrderService;
import com.buaa.aimall.goodsModule.Service.GoodsOrderService;
import com.buaa.aimall.goodsModule.VO.GoodsOrderAppealWorkOrderVO;
import com.buaa.aimall.userModule.Service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/goods/goods-order-appeal")
@CrossOrigin
public class GoodsOrderAppealWorkOrderController {

    @Autowired
    private GoodsOrderAppealWorkOrderService goodsOrderAppealWorkOrderService;

    @Autowired
    private UserService userService;

    @Autowired
    private GoodsOrderService goodsOrderService;

    @PostMapping("/order-appeal-application")
    public BaseResponse<Boolean> revenueAppeal(@Valid @RequestBody GoodsOrderAppealDTO request) {
        GoodsOrderAppealWorkOrderEntity goodsOrderAppealWorkOrderEntity = GoodsOrderAppealWorkOrderConvert.INSTANCE.createAppealWorkOrderToEntity(request);

        goodsOrderAppealWorkOrderEntity.setAppealerID(userService.getCurrentUser().getId());
        try {
            goodsOrderAppealWorkOrderService.createAppealWorkOrder(goodsOrderAppealWorkOrderEntity);
            return new BaseResponse<>(StatusCode.SUCCESS, "申诉成功", null);
        } catch (BusinessException ex) {
            return new BaseResponse<>(StatusCode.DAO_ERROR, ex.getMessage(), null);
        }
    }

    @PostMapping("/order-appeal-application-batch")
    public BaseResponse<Boolean> revenueAppealList(@Valid @RequestBody GoodsOrderAppealListDTO request) {
        for (String id : request.getOrderIDList()) {
            GoodsOrderAppealDTO goodsOrderAppealDTO = new GoodsOrderAppealDTO();
            goodsOrderAppealDTO.setContent(request.getContent());
            goodsOrderAppealDTO.setGoodsOrderID(id);
            GoodsOrderAppealWorkOrderEntity goodsOrderAppealWorkOrderEntity = GoodsOrderAppealWorkOrderConvert.INSTANCE.createAppealWorkOrderToEntity(goodsOrderAppealDTO);
            goodsOrderAppealWorkOrderEntity.setAppealerID(userService.getCurrentUser().getId());
            try {
                goodsOrderAppealWorkOrderService.createAppealWorkOrder(goodsOrderAppealWorkOrderEntity);
            } catch (BusinessException ex) {
                return new BaseResponse<>(StatusCode.DAO_ERROR, ex.getMessage(), null);
            }
        }
        return new BaseResponse<>(StatusCode.SUCCESS, "申诉成功", null);
    }

    @PostMapping("/process-order-appeal")
    public BaseResponse<Boolean> processAppeal(@Valid @RequestBody ProcessGoodsOrderAppealDTO processGoodsOrderAppealDTO) {
        try {
            goodsOrderAppealWorkOrderService.processAppeal(processGoodsOrderAppealDTO);
        } catch (BusinessException e) {
            return new BaseResponse<>(StatusCode.DAO_ERROR, e.getMessage(), null);
        }
        return new BaseResponse<>(StatusCode.SUCCESS, "申诉成功", null);
    }

    @PostMapping("/filter-appeal-order")
    public BaseResponse<PageReturnResult<GoodsOrderAppealWorkOrderEntity>> filterAppealWorkOrder(@Valid @RequestBody FilterGoodsOrderAppealWorkOrderDTO filterGoodsOrderAppealWorkOrderDTO) {
        PageReturnResult<GoodsOrderAppealWorkOrderEntity> workOrderList = goodsOrderAppealWorkOrderService.filterAppealWorkOrder(filterGoodsOrderAppealWorkOrderDTO);
        return new BaseResponse<>(StatusCode.SUCCESS, "查询成功", workOrderList);
    }

    @PostMapping("/filter-appeal-order-lite")
    public BaseResponse<Page<GoodsOrderAppealWorkOrderVO>> filterAppealWorkOrderLite(@RequestBody FilterGoodsOrderAppealWorkOrderLiteDTO dto) {
        Page<GoodsOrderAppealWorkOrderEntity> page = new Page<>(dto.getCurrentPage(), dto.getPageSize());

        LambdaQueryWrapper<GoodsOrderAppealWorkOrderEntity> queryWrapper = new LambdaQueryWrapper<>();
        if (dto.getGoodsType() != null) {
            queryWrapper.eq(GoodsOrderAppealWorkOrderEntity::getGoodsType, dto.getGoodsType());
        }
        if (dto.getStatus() != null) {
            queryWrapper.eq(GoodsOrderAppealWorkOrderEntity::getStatus, dto.getStatus());
        }
        if (dto.getContentKeyword() != null) {
            queryWrapper.like(GoodsOrderAppealWorkOrderEntity::getContent, dto.getContentKeyword());
        }
        if (dto.getCreateTimeStart() != null) {
            queryWrapper.ge(GoodsOrderAppealWorkOrderEntity::getCreateTime, dto.getCreateTimeStart());
        }
        if (dto.getCreateTimeEnd() != null) {
            queryWrapper.le(GoodsOrderAppealWorkOrderEntity::getCreateTime, dto.getCreateTimeEnd());
        }

        var resultPage = goodsOrderAppealWorkOrderService.page(page, queryWrapper);

        List<GoodsOrderAppealWorkOrderVO> voList = resultPage.getRecords().stream()
                .map(entity -> {
                    GoodsOrderAppealWorkOrderVO vo = new GoodsOrderAppealWorkOrderVO();
                    BeanUtils.copyProperties(entity, vo);
                    var goodsOrder = goodsOrderService.getById(entity.getGoodsOrderID());
                    vo.setGoodsOrder(goodsOrder);
                    return vo;
                })
                .collect(Collectors.toList());

        Page<GoodsOrderAppealWorkOrderVO> responsePage = new Page<>();
        responsePage.setCurrent(resultPage.getCurrent());
        responsePage.setSize(resultPage.getSize());
        responsePage.setTotal(resultPage.getTotal());
        responsePage.setPages(resultPage.getPages());
        responsePage.setRecords(voList);

        return new BaseResponse<>(StatusCode.SUCCESS, "查询订单申诉工单成功", responsePage);
    }
}
